package com.cyan.teamquestionnaire.controller;

import com.cyan.teamquestionnaire.beans.HttpResponseEntity;
import com.cyan.teamquestionnaire.entity.Group;
import com.cyan.teamquestionnaire.entity.Questionnaire;
import com.cyan.teamquestionnaire.service.AnswerSheetService;
import com.cyan.teamquestionnaire.service.GroupService;
import com.cyan.teamquestionnaire.service.QuestionnaireService;
import com.cyan.teamquestionnaire.utils.Constants;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
public class DataScreenController {

    // 返回信息
    @Resource
    private HttpResponseEntity httpResponseEntity;

    @Resource
    private GroupService groupService;

    @Resource
    AnswerSheetService answerSheetService;

    @Resource
    private QuestionnaireService questionnaireService;
    /**
     * 用户创建群组数量，传入用户的name，得到该用户所创建的群组总数
     * 不分页不模糊。
     * 传入name
     */
    @RequestMapping(value = "/userCreateGroupNumber", method = RequestMethod.POST)
    public HttpResponseEntity userCreateGroupNumber(@RequestBody Group group) {
        //将组名设置为空，防止后面模糊查询出现问题
        group.setGroupName("");
        httpResponseEntity.setData(groupService.getGroupList(group).size());
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }

    /**
     * 用户发布的所有的问卷的答卷率，传入用户的name，
     * 联合answer_sheet表和questionnaire表，分别得到该用户发布的所有问卷的已回答人数和未回答人数
     */
    @RequestMapping(value = "/userResponseRate", method = RequestMethod.POST)
    public HttpResponseEntity userResponseRate(String name) {

        httpResponseEntity.setData(answerSheetService.userResponseRate(name));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;

    }

    /**
     * 传入name。release_time
     * eg:  "releaseTime":"2022-11-13 22:09:46"
     *  以天为单位，传入某一天日期，在questionnaire表中得到发布问卷的总数
     * （包括发布中、已结束、和回收站的已结束，即release_time不为null）。我准备在数据大屏那动态显示近7天内发布问卷数的折线图
     */
    @RequestMapping(value = "/releaseTimeInToday", method = RequestMethod.POST)
    public HttpResponseEntity releaseTimeInToday(@RequestBody Map<String,Object> map) {

        httpResponseEntity.setData(questionnaireService.releaseTimeInToday(map));
        httpResponseEntity.setCode(Constants.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constants.STATUS_MESSAGE);
        return httpResponseEntity;
    }

}
